﻿using System;
using System.Linq;
using System.Web.Http;
using Breeze.WebApi;
using LunchLearn.WebApi.Loging;
using Newtonsoft.Json.Linq;
using LunchLearn.WebApi.Domain;
using LunchLearn.WebApi.Repository;

namespace LunchLearn.WebApi.Controllers
{
    [BreezeController]
    public class DataController : ApiController
    {
        private readonly IContextProvider _contextProvider;


        public DataController(IContextProvider contextProvider)
        {
            _contextProvider = contextProvider;
        }

        [HttpGet]
        public string Metadata()
        {
            return _contextProvider.Metadata();
        }

        [HttpGet]
        public IQueryable<Order> Orders()
        {
            return _contextProvider.Context.Orders.Include("Customer").Include("OrderLines").Include("OrderLines.Item");
        }

        [HttpGet]
        public IQueryable<Customer> Customers()
        {
            return _contextProvider.Context.Customers;
        }

        [HttpGet]
        public IQueryable<Item> Items()
        {
            return _contextProvider.Context.Items;
        }

        [HttpPost]
        public SaveResult SaveChanges(JObject saveBundle)
        {
            try
            {
                var result = _contextProvider.SaveChanges(saveBundle);
                return result;
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                throw;
            }
        }


    }
}